package com.mobileares.midp.widgets.ext;

import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.NotFoundException;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
import com.google.gwt.user.rebind.StringSourceWriter;

import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-11-4
 * Time: 8:32:13
 * To change this template use File | Settings | File Templates.
 */
public class PagesWrapper  extends Generator {
    private String className = null;
    private String packageName = null;

    public String generate(TreeLogger treeLogger, GeneratorContext generatorContext, String typeName) throws UnableToCompleteException {
        try {
            JClassType classType = generatorContext.getTypeOracle().getType(typeName);
            //
            packageName = classType.getPackage().getName();
            treeLogger.log(TreeLogger.DEBUG, "packageName:" + packageName);
            //
            className = "PagesFactoryWrapper";
            generateClass(treeLogger, generatorContext);
        } catch (Exception e) {
            treeLogger.log(TreeLogger.ERROR, "ERROR!!!", e);
        }
        return packageName + "." + className;
    }

    private void generateClass(TreeLogger logger, GeneratorContext context) {
        PrintWriter printWriter;
//        System.out.println("create---pageFactroy---");
        printWriter = context.tryCreate(logger, packageName, className);
        if (printWriter == null) return;
        ClassSourceFileComposerFactory composer = null;
        composer = new ClassSourceFileComposerFactory(packageName, className);
        composer.addImport("java.util.*");
        composer.addImport("com.google.gwt.core.client.*");

//        composer.addImport("com.nexustar.gwt.dashboard.client.AbstractPage");
        composer.setSuperclass("com.mobileares.midp.widgets.client.page.PagesFactory");
        StringSourceWriter bufferSourceWriter = new StringSourceWriter();


        bufferSourceWriter.println(" private  static Map  pages = new HashMap();");
        bufferSourceWriter.println(" private static boolean init = false;");
        bufferSourceWriter.indent();
        bufferSourceWriter.println("  public void getPage(final String mockPath, final String path, final PageClient client) {");

        bufferSourceWriter.println("  getPage(mockPath, new PageClient() {");
        bufferSourceWriter.println("       public void success(AbstractPage page) {");
        bufferSourceWriter.println("  if (page == null) {");
        bufferSourceWriter.println("  AbstractPage oPage = (AbstractPage) pages.get(path);");
        bufferSourceWriter.println("     AbstractPage newPage = oPage.getInstance();");
        bufferSourceWriter.println("   pages.put(mockPath, newPage);");
        bufferSourceWriter.println("    client.success(newPage);");
        bufferSourceWriter.println("   } else {");
        bufferSourceWriter.println("       client.success(page);");
        bufferSourceWriter.println(" }");
        bufferSourceWriter.println(" }");
        bufferSourceWriter.println("   public void failure() {");
        bufferSourceWriter.println("  client.failure();");
        bufferSourceWriter.println(" }");
        bufferSourceWriter.println("   });");
        bufferSourceWriter.println(" }");
        bufferSourceWriter.outdent();
        bufferSourceWriter.println("  public   void getPage(final String path,final PageClient client) {");
        bufferSourceWriter.println("    if(!init){");

        bufferSourceWriter.println("  GWT.runAsync(new RunAsyncCallback() {");
        bufferSourceWriter.println("  public void onFailure(Throwable err) {");
        bufferSourceWriter.println(" }");
        bufferSourceWriter.println("  public void onSuccess() {");


        try {
            JClassType superType = context.getTypeOracle().getType("com.mobileares.midp.widgets.client.page.AbstractPage");
            JClassType[] Types = superType.getSubtypes();
            int i = 0;
            for (JClassType type : Types) {
                if (type.isAbstract()) continue;
                String iClass = type.getPackage().getName() + "." + type.getName();
//                String tempClass = type.getName();
//                composer.addImport(iClass);
                bufferSourceWriter.println("AbstractPage tempPage" + i + "=new " + iClass + "();");
                bufferSourceWriter.println("pages.put(\"" + iClass + "\",tempPage" + i + ");");
                i++;
            }
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
//        bufferSourceWriter.println("     System.out.println(\"aaa\"); ");
        bufferSourceWriter.println("     init = true; ");
        bufferSourceWriter.println("    AbstractPage page = (AbstractPage) pages.get(path);");
        bufferSourceWriter.println("     client.success(page);");
        bufferSourceWriter.println("  }");
        bufferSourceWriter.println("   });");
        bufferSourceWriter.println("    }else{");
        bufferSourceWriter.println("    AbstractPage page = (AbstractPage) pages.get(path);");
        bufferSourceWriter.println("     client.success(page);");
        bufferSourceWriter.println("     }");

        bufferSourceWriter.outdent();
        bufferSourceWriter.println("}");
        bufferSourceWriter.outdent();
        bufferSourceWriter.println("}");

        SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
//        System.out.println(bufferSourceWriter.getBuffer().toString());
        sourceWriter.println(bufferSourceWriter.toString());
        //�ύд�������
        context.commit(logger, printWriter);
    }
}

